{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ab86cae7-74ed-4b84-a3ee-22446655f248",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter as tk\n",
    "from tkinter import filedialog, scrolledtext\n",
    "from PIL import Image, ImageTk\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "# 加载已训练好的模型\n",
    "# model = tf.keras.models.load_model('your_model_path.h5')\n",
    "class GarbageClassifierApp:\n",
    "    def __init__(self, root):\n",
    "        self.root = root\n",
    "        self.root.title(\"Garbage Classifier\")\n",
    "        self.root.geometry(\"800x400\")\n",
    "        # 顶部标签\n",
    "        self.header_label = tk.Label(self.root, text=\"Garbage Classifier\", font=(\"Helvetica\", 16, \"bold\"))\n",
    "        self.header_label.pack(pady=10)\n",
    "        # 图片显示区域\n",
    "        self.image_frame = tk.Frame(self.root, bg=\"lightgray\", width=300, height=300)\n",
    "        self.image_frame.pack(pady=10)\n",
    "        self.image_label = tk.Label(self.image_frame)\n",
    "        self.image_label.pack(padx=10, pady=10)\n",
    "        # 上传图片按钮\n",
    "        self.upload_button = tk.Button(self.root, text=\"上传图片\", command=self.upload_image)\n",
    "        self.upload_button.pack(pady=5)\n",
    "        # 分类结果显示区域\n",
    "        self.result_frame = tk.Frame(self.root)\n",
    "        self.result_frame.pack(pady=10)\n",
    "        self.result_label = tk.Label(self.result_frame, text=\"分类结果:\", font=(\"Helvetica\", 12, \"bold\"))\n",
    "        self.result_label.pack()\n",
    "        self.result_text = scrolledtext.ScrolledText(self.result_frame, wrap=tk.WORD, width=30, height=5)\n",
    "        self.result_text.pack()\n",
    "        # 退出按钮\n",
    "        self.quit_button = tk.Button(self.root, text=\"退出\", command=self.root.destroy)\n",
    "        self.quit_button.pack(pady=5)\n",
    "    def upload_image(self):\n",
    "        # 打开文件对话框，选择图片文件\n",
    "        file_path = filedialog.askopenfilename(filetypes=[(\"Image files\", \"*.jpg;*.jpeg;*.png;*.gif\")])\n",
    "        if file_path:\n",
    "            # 加载并显示所选图片\n",
    "            image = Image.open(file_path)\n",
    "            image = image.resize((300, 300))  # 调整大小以适应界面\n",
    "            photo = ImageTk.PhotoImage(image)\n",
    "            self.image_label.config(image=photo)\n",
    "            self.image_label.image = photo\n",
    "            # 对上传的图片进行分类\n",
    "            # 这里暂时使用随机分类结果作为示例\n",
    "            classes = ['硬纸板', '玻璃', '金属', '纸张','塑料']\n",
    "            predicted_class = np.random.choice(classes)  # 随机选择一个类别作为示例\n",
    "            self.result_text.insert(tk.END, f\"预测类别: {predicted_class}\\n\")\n",
    "            # 若要使用真实模型进行分类，请取消下一行的注释，并注释掉上一行\n",
    "            # predicted_class = self.classify_image(file_path)\n",
    "            # self.result_text.insert(tk.END, f\"预测类别: {predicted_class}\\n\")\n",
    "    def classify_image(self, image_path):\n",
    "        # 加载图片并进行预处理\n",
    "        img = Image.open(image_path)\n",
    "        img = img.resize((224, 224))  # 调整大小\n",
    "        img = np.array(img) / 255.0  # 归一化\n",
    "        img = np.expand_dims(img, axis=0)\n",
    "\n",
    "        # 进行分类预测\n",
    "        predictions = model.predict(img)\n",
    "        class_labels = ['硬纸板', '玻璃', '金属', '纸张','塑料']\n",
    "        predicted_class_index = np.argmax(predictions)\n",
    "        predicted_label = class_labels[predicted_class_index]\n",
    "\n",
    "        return predicted_label\n",
    "\n",
    "# 创建主窗口\n",
    "root = tk.Tk()\n",
    "app = GarbageClassifierApp(root)\n",
    "root.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4997cb6e-d2f9-4c1f-b7bb-4745ca0f339d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f05fefae-e5a4-4512-bef6-2734f474a5a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
